package com.common.stdlib
{
	import com.adobe.net.*;
	
	import flash.net.*;
	import flash.utils.*;
	
	//网络辅助
	public class NetUtil
	{
		
		public function NetUtil()
		{
		}
		
		//转换地址
		public static function inet_ntoa(dwServerAddr:uint):String
		{
			var b0:uint = dwServerAddr & 0x000000ff;
			var b1:uint = (dwServerAddr >> 8) & 0x000000ff;
			var b2:uint = (dwServerAddr >> 16)& 0x000000ff;
			var b3:uint = (dwServerAddr >> 24)& 0x000000ff;
			var result:String = b0 + "." + b1 + "." + b2 + "." + b3;
			return result;
		}
		
		//构建上传连接
		public static function structUploadURLString(server:String, filePath:String, 
		fileName:String, fileDataLen:uint):String
		{
			 var url:String = "http://" + server + "/uploadFile.php?filePath=" + filePath +
			"&fileName=" + fileName + "&fileDataLen=" + fileDataLen;
			
			return url;
		}
		
		//转换上传连接
		public static function convertUploadURLString(path:String, fileDataLen:uint):String
		{
			var uri:URI = new URI(path);
			var server:String = uri.authority;
		    var port:String = uri.port;
		    if(port)
		    	server = server + ":" + port;
		    var filePath:String = uri.path;
		    var fileName:String = uri.getFilename();
		    filePath = filePath.replace(fileName, "");
		    
			return structUploadURLString(server, filePath, fileName, fileDataLen);
		}
		
		//构建上传请求
		public static function structUploadURLRequest(server:String, filePath:String, 
		fileName:String, bytes:ByteArray):URLRequest
		{
			bytes.position = 0;
			
			 var urlRequest:URLRequest = new URLRequest(structUploadURLString(server, filePath, 
			fileName, bytes.length));
			urlRequest.data = bytes;
		    urlRequest.method = URLRequestMethod.POST;
			urlRequest.contentType = 'application/octet-stream';
			
			return urlRequest;
		}
		
		//转换上传连接
		public static function convertUploadURLRequest(path:String, bytes:ByteArray):URLRequest
		{
			bytes.position = 0;
			
			 var urlRequest:URLRequest = new URLRequest(convertUploadURLString(path, bytes.length));
			urlRequest.data = bytes;
		    urlRequest.method = URLRequestMethod.POST;
			urlRequest.contentType = 'application/octet-stream';
			
			return urlRequest;
		}
	}
}